 <?php
 $link = mysql_connect('localhost:3306', 'root', '');
 if (!$link){
 die('no connection');
 }
 $db_selected = mysql_select_db('shapes_colors', $link);
 if (!$db_selected) {
     die ('Can\'t use shapes-colors : ' . mysql_error());
 }
 
 
 $colortxt = 'pink';
 $shapetxt = 'triangle';
 
 if(!empty($_POST['coltxt']))
 {
 $colortxt = $_POST['coltxt'];
 }
 
 if(!empty($_POST['shapetxt']))
 {
 $shapetxt = $_POST['shapetxt'];
 }
 
 $sql = 'select id from colors where row_colors="'.$colortxt.'"';
 $sql2 = 'select id from shapes where row_shapes="'.$shapetxt.'"';
 
 $r1 = mysql_query($sql, $link);
 $r2 = mysql_query($sql2, $link);
 
 
 $var1 = mysql_fetch_row($r1);
 $var2 = mysql_fetch_row($r2);
 
 
 
 
 if (!$var1[0] or !$var2[0]){
 
     if (!$var1[0]){
 
     $sql = 'insert into colors (row_colors) value ("'.$colortxt.'")';
     $r3 = mysql_query($sql, $link);
 
     }
 
     if (!$var2[0]){
 
     $sql = 'insert into shapes (row_shapes) value ("'.$shapetxt.'")';
     $r3 = mysql_query($sql, $link);
 
     }
 
     mysql_query(sprintf('insert into colors_shapes (colors_id, shapes_id, time) values (1, 1, now())'));
 
 }
 else{
  
 mysql_query(sprintf('insert into colors_shapes  (colors_id, shapes_id, time) values ("%s", "%s", now())', $var1[0],  $var2[0]), $link);
 
 }
 
 ?>
 
 